package by.koleda.benicetobunnies.manager;

import java.util.ArrayList;
import java.util.List;

import by.koleda.benicetobunnies.model.Company;
import by.koleda.benicetobunnies.provider.BunniesContract;
import by.koleda.benicetobunnies.provider.BunniesContract.CompaniesColumns;

import android.content.ContentProviderOperation;
import android.content.ContentValues;
import android.content.Context;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.os.RemoteException;

public class CompaniesManager {
	
	public static void addCompanies(Context context, List<Company> p) {
		if (checkEntries(context) == true) {
			return;
		}
		ArrayList<ContentProviderOperation> companiesBatch = new ArrayList<ContentProviderOperation>();
		int counter = 0;
		for (int i = 0; i < p.size(); i++) {
			Company company = p.get(i);
			ContentValues contentValues = new ContentValues();
			contentValues.put(BunniesContract.CompaniesColumns.COMPANY_NAME, company.getCompanyName());
			contentValues.put(BunniesContract.CompaniesColumns.PARENT_COMPANY, company.getParentCompany());
			contentValues.put(BunniesContract.CompaniesColumns.ADDRESS, company.getAddress());
			contentValues.put(BunniesContract.CompaniesColumns.PHONE_NUMBER, company.getPhoneNumber());
			contentValues.put(BunniesContract.CompaniesColumns.LINK, company.getLink());
			companiesBatch.add(ContentProviderOperation.newInsert(BunniesContract.Companies.CONTENT_URI).withValues(contentValues).build());
			counter++;
			if (counter == 50 || i == p.size()) {
				try {
					context.getContentResolver().applyBatch(BunniesContract.CONTENT_AUTHORITY, companiesBatch);
					companiesBatch.clear();
					counter = 0;
				} catch (RemoteException e) {
					throw new RuntimeException("Problem applying batch operation", e);
				} catch (OperationApplicationException e) {
					throw new RuntimeException("Problem applying batch operation", e);
				}
			}
		}
	}
		
	public static boolean checkEntries(Context context) {
		Cursor cursor = context.getContentResolver().query(BunniesContract.Companies.CONTENT_URI, null, null, null, null);
		if (cursor.moveToFirst()) {
			return true;
		} else {
			return false;
		}
	}

	public static ArrayList<String> getCompanies(Context context) {
		ArrayList<String> companiesList = new ArrayList<String>();
		String[] projection = {
				CompaniesColumns.COMPANY_NAME
		};
		Cursor cursor = context.getContentResolver().query(BunniesContract.Companies.CONTENT_URI, projection, null, null, null);
		int companyColumnId = cursor.getColumnIndex(CompaniesColumns.COMPANY_NAME);
		
		
		while (cursor.moveToNext()) {
			String companyName = cursor.getString(companyColumnId);
			companiesList.add(companyName);
		}
		cursor.close();
		
		return companiesList;
	}
	
	public static ArrayList<String> getParentCompanies(Context context) {
		ArrayList<String> parentCompaniesList = new ArrayList<String>();
		String[] projection = {
				CompaniesColumns.PARENT_COMPANY
		};
		Cursor cursor = context.getContentResolver().query(BunniesContract.Companies.CONTENT_URI, projection, null, null, null);
		int companyColumnId = cursor.getColumnIndex(CompaniesColumns.PARENT_COMPANY);
		
		
		while (cursor.moveToNext()) {
			String companyName = cursor.getString(companyColumnId);
			parentCompaniesList.add(companyName);
		}
		cursor.close();
		
		return parentCompaniesList;
	}
	
	public static ArrayList<String> createDictionary(String companyName) {
		if (companyName == "") {
			return null;
		}
		ArrayList<String> dictionary = new ArrayList<String>();
		String[] words = null;
		String delimeter = " ";
		words = companyName.split(delimeter);
		String word = "";
		for (String entry: words) {
			word += entry;
			dictionary.add(word);
			word += " ";
		}
		return dictionary;
	}
}
